package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.service.*;
import com.example.demo.bean.ApiResponse;
import com.example.demo.entity.*;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    @GetMapping("/users")
    public ResponseEntity<ApiResponse<Page<User>>> getUsers(@RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {

        try {
            Page<User> list = userService.getUsers(page, size);
            ApiResponse<Page<User>> response = new ApiResponse<Page<User>>(true, "User fetched successfully", list);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            ApiResponse<Page<User>> response = new ApiResponse<>(false, e.getMessage(), null);
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/users/{id}")
    public ResponseEntity<ApiResponse<User>> getUserById(@PathVariable String id) {
        try {
            User user = userService.getUserById(id);
            ApiResponse<User> response = new ApiResponse<>(true, "User fetched successfully", user);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            ApiResponse<User> response = new ApiResponse<>(false, e.getMessage(), null);
            return ResponseEntity.badRequest().body(response);
        }
    }

    @PostMapping("/users/update")
    public ResponseEntity<ApiResponse<User>> updateUserByForm(@RequestBody User user) {
        try {
            User resultUser = userService.updateUser(user);
            ApiResponse<User> response = new ApiResponse<>(true, "User fetched successfully", resultUser);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            ApiResponse<User> response = new ApiResponse<>(false, e.getMessage(), null);
            return ResponseEntity.badRequest().body(response);
        }
    }

    @GetMapping("/users/delete")
    public ResponseEntity<ApiResponse<User>> deleteUserById(@RequestParam("id") Long id) {
        try {
             userService.deleteUserById(id);
            ApiResponse<User> response = new ApiResponse<>(true, "删除成功", null);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            ApiResponse<User> response = new ApiResponse<>(false, e.getMessage(), null);
            return ResponseEntity.badRequest().body(response);
        }
    }
}